<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chk = new CheckMode_3d($curruser['acl'], 3, $gametype);
$chk->checkShow3dPage();
if ($chk->show3D3S != 0)
{
    exit("error show3D3S");
}
unset($chk);
if (SubAccountRight($curruser, 3) != 2)
{
    exit("error right!");
}
$ui = new UI_3D(true);
$tpl = new Template2();
$conf = new Config();
$tbl = $g_gametype_3d[$gametype]['en'];
$tblName = $g_gametype_3d[$gametype]['ch'];
if ($conf->get("calcbet_{$tbl}") == "1")
{
    $ui->showErrorPage("{$tblName}颁奖过帐中，请稍後再使用本功能！");
    exit();
}
if ($conf->get("cnyDateS") == "" || $conf->get("cnyDateE") == "")
{
    $ui->showErrorPage("请先至[系统]页面设定[春节休市期间]後再使用本功能！");
    exit();
}
include_once( "class_3d/MyLotto_3d.php" );
include_once( "class/function_3d.php" );
$MyLotto_3d = new MyLotto_3d($gametype);
$cmd = getvar("cmd");
$id = intval(getvar("id"));
$idstr = $id == "" ? "" : "&id=" . $id;
if (ispost())
{
    $cnyDateSInt = strtotime($conf->get("cnyDateS") . " 00:00:00");
    $cnyDateEInt = strtotime($conf->get("cnyDateE") . " 23:59:59");
    $reUrl = "op.php?op=admin&fp=lotto_3d&gametype={$gametype}&cmd=" . $cmd . $idstr;
    $round = getvar("round");
    $open = getvar("open");
    $n1 = getvar("n1");
    $n2 = getvar("n2");
    $n3 = getvar("n3");
    if ($open == "0000-00-00")
    {
        
    }
    else if (!is_date($open))
    {
        diemsg("{$tblName}资料输入错误", "不正确的日期格式", $reUrl);
    }
    else
    {
        if ($round == "")
        {
            diemsg("{$tblName}资料输入错误", "期数不可空白", $reUrl);
        }
        else
        {
            if ($open == "")
            {
                diemsg("{$tblName}资料输入错误", "期不可空白", $reUrl);
            }
            if (!$MyLotto_3d->checkNum($n3))
            {
                diemsg("{$tblName}资料输入错误", "佰位数开奖号码限 0 - 9 之间的数字", $reUrl);
            }
            else if (!$MyLotto_3d->checkNum($n2))
            {
                diemsg("{$tblName}资料输入错误", "拾位数开奖号码限 0 - 9 之间的数字", $reUrl);
            }
            else if (!$MyLotto_3d->checkNum($n1))
            {
                diemsg("{$tblName}资料输入错误", "个位数开奖号码限 0 - 9 之间的数字", $reUrl);
            }
        }
    }
    $n1 = $n1 === "" ? "-1" : intval($n1);
    $n2 = $n2 === "" ? "-1" : intval($n2);
    $n3 = $n3 === "" ? "-1" : intval($n3);
    $updateData = array();
    if ($n1 != 0 - 1 && $n2 != 0 - 1 && $n3 != 0 - 1)
    {
        $nArr = array($n1, $n2, $n3);
        sort($nArr);
        $updateData = $MyLotto_3d->calOpenNum($n3, $n2, $n1);
    }
    $updateData['round'] = $round;
    $updateData['open'] = $open;
    $updateData['n3'] = $n3;
    $updateData['n2'] = $n2;
    $updateData['n1'] = $n1;
    $openbetChkInt = strtotime($updateData['open']);
    if ($cnyDateSInt <= $openbetChkInt && $openbetChkInt <= $cnyDateEInt)
    {
        diemsg("{$tblName}资料输入错误", "开奖日期不可介於春节休市期间。", $reUrl);
    }
    switch ($cmd)
    {
        case "add" :
            if ($MyLotto_3d->checkRoundOpen($round, $open))
            {
                diemsg("{$tblName}资料输入错误", "开奖日期已存在。", $reUrl);
            }
            if ($lotto_id = $MyLotto_3d->add($updateData))
            {
                $MyLotto_3d->copyRateZeroToN($lotto_id);
                $MyLotto_3d->createTblBettotal($lotto_id);
                header("location:op.php?op=admin&fp=history_3d&gametype={$gametype}");
                exit();
            }
            else
            {
                diemsg("{$tblName}新增失败", "新增{$tblName}期数资料时发生问题", $reUrl);
            }
            break;
        case "edit" :
            $id = intval(getvar("id"));
            if (empty($id))
            {
                diemsg("{$tblName}资料输入错误", "错误的ID值");
            }
            $id = intval($id);
            $rowChk = $MyLotto_3d->getDataById($id);
            if ($rowChk === false)
            {
                diemsg("{$tblName}资料输入错误", "错误的ID值2");
            }
            $upFlag = true;
            if ($curruser['account'] != G_SUPERACCOUNT && $curruser['id'] != 1 && in_array($rowChk['n3'], array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)) && in_array($rowChk['n2'], array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)) && in_array($rowChk['n1'], array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)))
            {
                $upFlag = false;
                diemsg("{$tblName}目前不可更改资料", "更新失败", "op.php?op=admin&fp=history_3d&gametype={$gametype}");
            }
            if ($upFlag)
            {
                $MyLotto_3d->update($updateData, $id);
                if ($updateData['open'] == "0000-00-00")
                {
                    header("location:op.php?op=admin&fp=history_3d&gametype={$gametype}");
                    exit();
                }
                if ($n3 == 0 - 1 && $n2 == 0 - 1 && $n1 == 0 - 1)
                {
                    include( "class_3d/class.lotto3d.php" );
                    $ClassLotto3D = new lotto3d($gametype);
                    $ClassLotto3D->ClearBetData($id);
                }
            }
            break;
    }
}
$round = "";
$open = "";
$n3 = "";
$n2 = "";
$n1 = "";
switch ($cmd)
{
    case "edit" :
        $id = getvar("id");
        if (empty($id))
        {
            diemsg("{$tblName}资料输入错误", "错误的ID值", $reUrl);
        }
        $id = intval($id);
        $row = $MyLotto_3d->getDataById($id);
        if ($row === false)
        {
            diemsg("{$tblName}资料输入错误", "错误的ID值2", $reUrl);
        }
        $round = $row['round'];
        $open = $row['open'];
        $n3 = $row['n3'];
        $n2 = $row['n2'];
        $n1 = $row['n1'];
        $postChkTime = $conf->get("paper_post_bet");
        $todayChk = date("Hi") <= $postChkTime ? date("Y-m-d", strtotime("-1 day")) : date("Y-m-d");
        if ($todayChk == $open)
        {
            $x = "";
        }
        else
        {
            $x = "x";
        }
        $rs = getdb("select count(amount) from {$x}bet_{$tbl} where lotto_id={$id}");
        $rs1 = getdb("select count(amount) from {$x}bet2_{$tbl} where lotto_id={$id}");
        $nn = 400;
        $totalN = $rs->f[0] + $rs1->f[0];
        if (30000 < $totalN)
        {
            $nn = 1600;
        }
        else if (25000 < $totalN)
        {
            $nn = 1400;
        }
        else if (20000 < $totalN)
        {
            $nn = 1200;
        }
        else if (15000 < $totalN)
        {
            $nn = 1000;
        }
        else if (10000 < $totalN)
        {
            $nn = 800;
        }
        else if (5000 < $totalN)
        {
            $nn = 600;
        }
        $tpl->set("bet_count1", sprintf("%.0f", $totalN / $nn) + 1);
        $tpl->set("bet_count2", $totalN);
        break;
    case "add" :
        if ($round = $MyLotto_3d->getLotto_3d_id())
        {
            $round = $round + 1;
        }
        else
        {
            $round = 1;
        }
        $open = date("Y-m-d");
        break;
}
$n1 = $n1 == 0 - 1 ? "-1" : $n1;
$n2 = $n2 == 0 - 1 ? "-1" : $n2;
$n3 = $n3 == 0 - 1 ? "-1" : $n3;
$openFlag = $MyLotto_3d->checkOpen($n3, $n2, $n1);
$tpl->set("cmd", $cmd);
$tpl->set("round", $round);
$tpl->set("open", $open);
$tpl->set("n3", $n3);
$tpl->set("n2", $n2);
$tpl->set("n1", $n1);
$tpl->set("id", $id);
$tpl->set("gametype", $gametype);
$tpl->set("editType", "false");
if (!$openFlag || $conf->get("calcbet_{$tbl}") == "1")
{
    $tpl->set("btnDisCalc", "disabled=\"disabled\"");
}
else
{
    $today = explode(" ", date("Y-m-d Hi"));
    if ($conf->get("stopbet_{$tbl}") == "0000")
    {
        $stopbet_3d = "2359";
    }
    else
    {
        $stopbet_3d = "0000";
    }
    if ($today[0] == $open && $conf->get("startbet_{$tbl}") < $today[1] && $today[1] < $stopbet_3d)
    {
        $tpl->set("btnDisStopPlay", "");
    }
    else
    {
        $tpl->set("btnDisStopPlay", "disabled=\"disabled\"");
    }
}
$tpl->load("main", "lotto_3d.htm");
$ui->header();
echo title("{$tblName} 新增期数管理", "<a href=\"op.php?op=admin&fp=history_3d&gametype={$gametype}\">列表</a>");
echo "<br />";
$ui->load("calendar_js" , "calendar_js.htm");
$ui->show("calendar_js");
$tpl->show("main");
$ui->footer();
?>